package com.ruoyi.order.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.order.mapper.CropTypeMapper;
import com.ruoyi.order.domain.Customer;
import com.ruoyi.order.mapper.CustomerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.order.mapper.OrderMapper;
import com.ruoyi.order.domain.Order;
import com.ruoyi.order.service.IOrderService;

/**
 * 订单Service业务层处理
 *
 * @author testlove
 * @date 2021-07-08
 */
@Service
public class OrderServiceImpl implements IOrderService
{
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private CropTypeMapper cropTypeMapper;
    @Autowired
    private CustomerMapper customerMapper;

    /**
     * 查询订单
     *
     * @param orderId 订单ID
     * @return 订单
     */
    @Override
    public Order selectOrderById(String orderId)

    {
        Order order = orderMapper.selectOrderById(orderId);
        return  order;
    }

    /**
     * 查询订单列表
     *
     * @param order 订单
     * @return 订单
     */
    @Override
    public List<Order> selectOrderList(Order order, List<SysRole> roles)
    {
        boolean flag = false;
        for (SysRole role : roles) {
            if(role.getRoleName().equals("总经理")||role.getRoleName().equals("财务采购经理")||role.getRoleName().equals("销售经理")){
                flag=true;
            break;
        }
        }
        List<Order> answer = new ArrayList<>();
        List<Order> orders;
        if(!flag){
            orders = orderMapper.selectOrderListWithoutPrice(order);
            System.out.println("原始列表"+orders);
            for (Order order1:orders){
                order1.setCropType(cropTypeMapper.selectCropTypeById(order1.getCropTypeId()).getCropType());
                order1.setCustomer(customerMapper.selectCustomerById(order1.getCustomerId()).getContactPerson());

            }
            System.out.println("订单列表"+answer);
            return orders;

        }
        orders = orderMapper.selectOrderList(order);
        System.out.println("原始列表"+orders);
        for (Order order1:orders){
            order1.setCropType(cropTypeMapper.selectCropTypeById(order1.getCropTypeId()).getCropType());
            order1.setCustomer(customerMapper.selectCustomerById(order1.getCustomerId()).getContactPerson());

        }
        System.out.println("订单列表"+answer);

        return orders;
    }

    /**
     * 新增订单
     *
     * @param order 订单
     * @return 结果
     */
    @Override
    public int insertOrder(Order order)
    {
        order.setCreateTime(DateUtils.getNowDate());
        if(order.getOrderId()!=null) {
            return orderMapper.insertOrder(order);
        }
        Customer customer = customerMapper.selectCustomerByCompanyName(order.getCustomer());
        if (customer != null) {
            order.setCustomerId(customer.getCustomerId());
            return orderMapper.insertOrder(order);
        }
        return 0;
    }

    /**
     * 修改订单
     *
     * @param order 订单
     * @return 结果
     */
    @Override
    public int updateOrder(Order order)
    {
        order.setUpdateTime(DateUtils.getNowDate());
        Order oldOrder = orderMapper.selectOrderById(order.getOrderId());
        oldOrder.setUpdateTime(order.getUpdateTime());
        oldOrder.setUpdateBy(order.getUpdateBy());
        oldOrder.setStatus(order.getStatus());
        return orderMapper.updateOrder(oldOrder);
    }

    /**
     * 批量删除订单
     *
     * @param orderIds 需要删除的订单ID
     * @return 结果
     */
    @Override
    public int deleteOrderByIds(String[] orderIds)
    {
        return orderMapper.deleteOrderByIds(orderIds);
    }

    /**
     * 删除订单信息
     *
     * @param orderId 订单ID
     * @return 结果
     */
    @Override
    public int deleteOrderById(String orderId)
    {
        return orderMapper.deleteOrderById(orderId);
    }
}
